package me.zhengjie.modules.website.service.impl;

import me.zhengjie.modules.website.domain.ConversationTable;
import me.zhengjie.modules.website.repository.ConversationTableRepository;
import me.zhengjie.modules.website.repository.ConversationTableSpecifications;
import me.zhengjie.modules.website.service.ConversationTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import me.zhengjie.modules.website.domain.PageResult;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.criteria.Predicate;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.sql.Timestamp;
import java.time.LocalDateTime;

/**
 * @author system
 * @since 2024-10-23 22:27:03
 */
@Service
public class ConversationTableServiceImpl implements ConversationTableService {

    @Autowired
    private ConversationTableRepository conversationTableRepository;




    @Override
    public PageResult<ConversationTable> findPage(ConversationTable conversationTable, LocalDateTime startDate, LocalDateTime endDate, Pageable pageable) {
        Page<ConversationTable> page = conversationTableRepository.findAll((root, query, criteriaBuilder) -> {
            List<Predicate> predicates = new ArrayList<>();

            // 通过反射遍历 ConversationTable 的所有字段，过滤静态字段
            for (Field field : ConversationTable.class.getDeclaredFields()) {
                field.setAccessible(true); // 允许访问私有字段

                // 跳过静态字段
                if (Modifier.isStatic(field.getModifiers())) {
                    continue;
                }

                try {
                    Object value = field.get(conversationTable); // 获取字段值
                    if (value != null) { // 如果字段不为空，添加查询条件
                        predicates.add(criteriaBuilder.equal(root.get(field.getName()), value));
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }

            // 时间范围条件
            if (startDate != null) {
                predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get("createTime"), Timestamp.valueOf(startDate)));
            }
            if (endDate != null) {
                predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get("createTime"), Timestamp.valueOf(endDate)));
            }

            return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
        }, pageable);

        PageResult<ConversationTable> result = new PageResult<>();
        result.setContent(page.getContent());
        result.setTotalElements(page.getTotalElements());
        return result;
    }


    @Override
    public List<ConversationTable> findList(ConversationTable conversationTable) {
        return conversationTableRepository.findAll(ConversationTableSpecifications.queryList(conversationTable));
    }

    @Override
    public ConversationTable findById(Long id) {
        return conversationTableRepository.findById(id).orElse(null);
    }

    @Override
    public ConversationTable insert(ConversationTable conversationTable) {
        return conversationTableRepository.save(conversationTable);
    }

    @Override
    public ConversationTable update(ConversationTable conversationTable) {
        return conversationTableRepository.saveAndFlush(conversationTable);
    }

    @Override
    public void delete(Long id) {
        conversationTableRepository.deleteById(id);
    }

}